﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using ALDIOrganizer.Properties;

namespace ALDIOrganizer
{    
    public partial class Rota
    {
        public void RecomputeTotalHoursField()
        {
            if (IsEmpty)
            {
                TotalHoursString = String.Empty;
            }
            else
            {
                var total = TotalHours()[7];
                TotalHoursString = total.OnProductivity +
                    (total.OffProductivity > 0 ?
                    String.Concat(" (+",
                    total.OffProductivity,
                    Resources.POSITION_SHORT_OffProd,
                    ")")
                    : String.Empty);
            }
        }

        public void RecomputeWhoOnHolidaysField()
        {
            var result = new List<String>();
            if (OnHolidaysRETURNING.Count > 0)
            {
                result.Add(
                    GetFirstNames(OnHolidaysRETURNING.ToArray())
                    + Resources.VACATIONIST_Returning);
            }
            if (OnHolidaysALLWEEK.Count > 0)
            {
                result.Add(
                    GetFirstNames(OnHolidaysALLWEEK.ToArray())
                    + Resources.VACATIONIST_AllWeek);
            }
            if (OnHolidaysLEAVING.Count > 0)
            {
                result.Add(
                    GetFirstNames(OnHolidaysLEAVING.ToArray())
                    + Resources.VACATIONIST_Leaving);
            }
            if (OnHolidaysPARTLY.Count > 0)
            {
                result.Add(
                    GetFirstNames(OnHolidaysPARTLY.ToArray())
                    + Resources.VACATIONIST_PartlyAway);
            }
        

            WhoOnHolidays = String.Join(
                "; ",
                result.ToArray());
        }

        

        #region for the use of DataFile class
        internal List<Int64> OnHolidaysALLWEEK;
        internal List<Int64> OnHolidaysRETURNING;
        internal List<Int64> OnHolidaysLEAVING;
        internal List<Int64> OnHolidaysPARTLY;

        internal void InitializeHolidaysLists()
        {
            OnHolidaysALLWEEK = new List<long>();
            OnHolidaysLEAVING = new List<long>();
            OnHolidaysPARTLY = new List<long>();
            OnHolidaysRETURNING = new List<long>();
        }

        internal void ClearHolidayLists()
        {
            OnHolidaysALLWEEK.Clear();
            OnHolidaysLEAVING.Clear();
            OnHolidaysPARTLY.Clear();
            OnHolidaysRETURNING.Clear();
        }

        internal void RemoveFromHolidayLists(Int64 pWhom)
        {
            var _lists = new[] { OnHolidaysALLWEEK,
                OnHolidaysLEAVING, OnHolidaysPARTLY,
                OnHolidaysRETURNING };
            foreach (var _list in _lists.Where(_list => _list.Contains(pWhom)))
            {
                _list.Remove(pWhom);
            }
        }

        string GetFirstNames(Int64[] pIDs)
        {
            var _list = new List<string>();
            foreach (var _id in pIDs)
            {
                _list.Add(SQLiteAccess.GetEmployeeFirstName(_id));
            }
            if (_list.Count == 0)
            {
                return String.Empty;
            }
            _list.Sort();
            return String.Join(", ",
                _list.ToArray()) + " - ";
        }
        #endregion
    }
}
